package com.zxw.user.service.impl;

import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.zxw.common.core.base.enums.ResponseEnum;
import com.zxw.common.core.exception.AppException;
import com.zxw.common.web.annotation.InitInsertInfo;
import com.zxw.common.web.annotation.InitSelectInfo;
import com.zxw.common.web.annotation.InitUpdateInfo;
import com.zxw.common.web.util.JwtUtils;
import com.zxw.user.domain.entity.Button;
import com.zxw.user.mapper.ButtonMapper;
import com.zxw.user.service.ButtonService;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author Xuewei.Zhang
 * @since 2022-05-04
 */
@Service
@RequiredArgsConstructor
public class ButtonServiceImpl extends ServiceImpl<ButtonMapper, Button> implements ButtonService {

    private final ButtonMapper buttonMapper;

    @Override
    @InitSelectInfo
    public List<Button> getAllByCompanyIdAndMenuId(Button button) {
        return buttonMapper.getAllByCompanyIdAndMenuId(button);
    }

    @Override
    public Button getButton(Long id) {
        Button button =  buttonMapper.selectById(id);
        return button;
    }

    @Override
    @InitInsertInfo
    public Long addButton(Button button) {
        long id = buttonMapper.insert(button);
        return id;
    }

    @Override
    @InitUpdateInfo
    public Button updateButton(Button button) {
        if(ObjectUtil.isEmpty(button.getId()))
            throw new AppException(ResponseEnum.PRIMARY_KEY_CANNOT_BE_EMPTY);
        buttonMapper.updateById(button);
        return button;
    }

    @Override
    public void delete(String ids) {
        Long companyId = JwtUtils.getCompanyId();
        QueryWrapper<Button> queryWrapper = new QueryWrapper();
        queryWrapper.in("id",ids);
        queryWrapper.eq("company_id",companyId);
        buttonMapper.delete(queryWrapper);
    }


}
